﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Web.Interfaces;

namespace Cofe.Web.Implements
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEWS)]
    public class StreamRequestFormatter : IHttpRequestMessageFormatter
    {
        public bool CanCreateRequestObject(HttpRequestMessage request, Type outputType, ParameterDic parameters)
        {
            return (outputType == typeof(StreamContainer) || outputType == typeof(IEnumerable<StreamContainer>));
        }

        public object CreateRequestObject(HttpRequestMessage request, Type outputType, ParameterDic parameters)
        {
            if (CanCreateRequestObject(request, outputType, parameters))
            {
                //if (System.Web.HttpContext.Current
                //request.Headers.fil
                return StreamContainer.FromIOStream(request.Content.ReadAsStreamAsync().Result, null);
            }

            throw new NotSupportedException();
        }
    }
}
